package com.wedu.modules.dss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.Query;
import com.wedu.modules.dss.dao.DeviceInformationDao;
import com.wedu.modules.dss.dao.MaintenanceRecordsDao;
import com.wedu.modules.dss.dao.MaintenanceWarningDao;
import com.wedu.modules.dss.entity.dto.MaintenanceRecordsDTO;
import com.wedu.modules.dss.entity.DeviceInformationEntity;
import com.wedu.modules.dss.entity.MaintenanceRecordsEntity;
import com.wedu.modules.dss.service.MaintenanceRecordsService;
import com.wedu.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

@Service("MaintenanceRecordsService")
public class MaintenanceRecordsServiceImpl extends ServiceImpl<MaintenanceRecordsDao, MaintenanceRecordsEntity> implements MaintenanceRecordsService {

    @Autowired
    private MaintenanceRecordsDao maintenanceRecordsDao;

    @Autowired
    private DeviceInformationDao deviceInformationDao;

    @Autowired
    private MaintenanceWarningDao maintenanceWarningDao;

    @Override
    public PageUtils queryPageRecord(Map<String, Object> params) {
        String deviceId = (String) params.get("deviceId");
        String workshopName = (String) params.get("workshopName");
        String deviceType = (String) params.get("deviceType");
        String deviceName = (String) params.get("deviceName");

        MPJLambdaWrapper<MaintenanceRecordsEntity> wrapper = new MPJLambdaWrapper<MaintenanceRecordsEntity>()
                .select(MaintenanceRecordsEntity::getDeviceId,MaintenanceRecordsEntity::getId,MaintenanceRecordsEntity::getMaintenanceTime,MaintenanceRecordsEntity::getUserId)
                .select(DeviceInformationEntity::getDeviceName)
                .select(SysUserEntity::getUsername)
                .leftJoin(DeviceInformationEntity.class,DeviceInformationEntity::getDeviceId,MaintenanceRecordsEntity::getDeviceId)
                .leftJoin(SysUserEntity.class,SysUserEntity::getUserId,MaintenanceRecordsEntity::getUserId)
                .like(StringUtils.isNotBlank(deviceName),"device_name",deviceName)
                .orderByDesc("maintenance_time");
        IPage<MaintenanceRecordsEntity> page = this.page(
                new Query<MaintenanceRecordsEntity>().getPage(params),
                wrapper
        );
        IPage<MaintenanceRecordsDTO> maintenanceRecordsDTOIPage = new Page<>();
        maintenanceRecordsDTOIPage.setTotal(page.getTotal());
        maintenanceRecordsDTOIPage.setSize(page.getSize());
        maintenanceRecordsDTOIPage.setCurrent(page.getCurrent());
        maintenanceRecordsDTOIPage.setPages(page.getPages());
        IPage<MaintenanceRecordsDTO> maintenanceRecordsDTOS = maintenanceRecordsDao.selectJoinPage(maintenanceRecordsDTOIPage,MaintenanceRecordsDTO.class,wrapper);

        return new PageUtils(maintenanceRecordsDTOS);
    }


    @Override
    public void deleteddevice(Long[] ids) {
            this.removeByIds(Arrays.asList(ids));
    }



    @Override
    public MaintenanceRecordsEntity getByDeviceId(String deviceId) {
        QueryWrapper<MaintenanceRecordsEntity> wrapper = new QueryWrapper<MaintenanceRecordsEntity>()
                .eq("device_id",deviceId);

        MaintenanceRecordsEntity maintenanceRecordsEntity = maintenanceRecordsDao.selectOne(wrapper);
        return maintenanceRecordsEntity;
    }
}
